import axios from 'axios'

//axios.defaults.baseURL = "/api";
//axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
axios.defaults.headers.post['Content-Type'] = 'application/json';
axios.defaults.headers.post['Accept'] = 'application/json';

axios.defaults.timeout = 20000;
// 创建实例时设置配置的默认值
// var instance = axios.create({
//     baseURL: "/api"
// });

// 在实例已创建后修改默认值
//instance.defaults.headers.common['Authorization'] = AUTH_TOKEN;

// 添加请求拦截器
axios.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么
    return config;
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
    // 对响应数据做点什么
    return Promise.resolve(response);
}, function (error) {
    // 对响应错误做点什么
    return Promise.reject(error);
});
/**
 * get方法，对应get请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
const bpath = '/api/';
export function get(url, param) {
    return new Promise((resolve, reject) => {
        axios.get(bpath + url, {
            params: param,
        }).then(res => {
            resolve(res.data);
        }).catch(err => {
            reject(err.data)
        })
    });
}
export function deleteFn(url, param) {
    return new Promise((resolve, reject) => {
        axios.delete(bpath + url, {
            params: param,
        }).then(res => {
            resolve(res.data);
        }).catch(err => {
            reject(err.data)
        })
    });
}
export function getAllURL(url, param) {
    return new Promise((resolve, reject) => {
        axios.get(url, {
            params: param,
        }).then(res => {
            resolve(res.data);
        }).catch(err => {
            reject(err.data)
        })
    });
}

/** 
 * post方法，对应post请求 
 * @param {String} url [请求的url地址] 
 * @param {Object} params [请求时携带的参数] 
 */
export function post(url, params,headers) {
    return new Promise((resolve, reject) => {
        axios({
            url,
            data: params,
            method: 'post',
            headers
        }).then(res => {
            resolve(res.data);
        }).catch(err => {
            reject(err.data)
        })
    });
}
export function postAllURL(url, params,headers) {
    return new Promise((resolve, reject) => {
        axios({
            url,
            data: params,
            method: 'post',
            headers
        }).then(res => {
            resolve(res.data);
        }).catch(err => {
            reject(err.data)
        })
    });
}
// export function post(url, params) {
//     return new Promise((resolve, reject) => {
//         axios.post("http://flowise.bdair/api/v1/prediction/a92dc936-5295-442e-92fc-0af3119f576d", params)
//             .then(res => {
//                 resolve(res.data);
//             })
//             .catch(err => {
//                 reject(err.data)
//             })
//     });
// }

